package jobhunter.controller;

import java.io.UnsupportedEncodingException;
import java.util.GregorianCalendar;

import javax.servlet.http.HttpSession;

import jobhunter.model.Action;
import jobhunter.model.User;
import jobhunter.model.enums.ActionState;
import jobhunter.model.enums.ActionType;
import jobhunter.model.enums.CommunicationType;
import jobhunter.repository.ActionRepository;
import jobhunter.repository.CompanyRepository;
import jobhunter.repository.ContactRepository;
import jobhunter.repository.ProcessRepository;
import jobhunter.security.SecurityContext;
import jobhunter.service.ActionService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ActionController {
	
	@Autowired	private ActionService actionService;
	

	@Autowired private ActionRepository actionRepository;
	@Autowired	private ContactRepository contactRepository;
	@Autowired	private ProcessRepository processRepository;

	@Autowired private CompanyRepository companyRepository;

	@RequestMapping("/actionsList")
	public ModelAndView actionsList(HttpSession session)
	{
		ModelAndView mav = new ModelAndView("action/actionsList");
		User user = null;
		mav.addObject("actionsList", actionRepository.findByUser());
		//System.out.println(session.getAttribute("UserId"));
		return mav;
	}


	@RequestMapping("/action")
	public ModelAndView action(@RequestParam(value = "id") Long id, @RequestParam(value = "e", required = false) Long e)
	{
		Action action = actionRepository.findById(id);	
		ModelAndView mav = new ModelAndView("action/action");
		mav.addObject("action", action);
		mav.addObject("typeError",e);
		return mav;
	}

	@RequestMapping("/delAction")
	public String delAction(@RequestParam(value = "id") Long id,
			@RequestParam(value = "compId", required = false) Long compId,
			@RequestParam(value = "procId", required = false) Long procId,
			@RequestParam(value = "contId", required = false) Long contId)
	{
		
		
		
		try
		{
			actionRepository.remove(id);
			if(compId != null )
			{
				return "redirect:company?id="+compId;
			}
			else if(procId!=null)
			{
				return "redirect:company?id="+procId;
			}
			else if(contId!=null)
			{
				return "redirect:contact?id="+contId;
				
			}
			else
			{
				return "redirect:actionsList";				
			}			
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
			return "redirect:action?id="+id+"&e=1";
		}
	}

	@RequestMapping("/addAction")
	public ModelAndView addAction(@RequestParam(value = "compId", required = false) Long compId,
			@RequestParam(value = "procId", required = false) Long procId,
			@RequestParam(value = "contId", required = false) Long contId)
	{
		ModelAndView mav = new ModelAndView("action/addAction");


		if(procId != null )
		{
			mav.addObject("process",processRepository.findById(procId));
			if(compId != null )
			{
				mav.addObject("company",companyRepository.findById(compId));
			}
		}
		else
		{
			mav.addObject("processes",processRepository.findByUser());
		}
		
		if(contId != null)
		{
			mav.addObject("contact",contactRepository.findById(contId));;
			
		}

		mav.addObject("contacts",contactRepository.findByUser());
		return mav;
	}

	@RequestMapping("/addActionValidate")
	public String addActionValidate(@RequestParam(value = "name") String name,
			@RequestParam(value = "realized") int realized,
			@RequestParam(value = "cv") String cv,
			@RequestParam(value = "motivationLettre") String motivationLettre,
			@RequestParam(value = "description") String description,
			@RequestParam(value = "actionType") ActionType actionType,
			@RequestParam(value = "actionState") ActionState actionState,
			@RequestParam(value = "communicationType") CommunicationType communicationType,
			@RequestParam(value = "processId") Long processId,
			@RequestParam(value = "contactId") Long contactId,
			@RequestParam(value = "companyId", required = false) Long compId,
			@RequestParam(value = "processId", required = false) Long procId,
			@RequestParam(value = "companyId", required = false) Long companyId,
			@RequestParam(value = "page", required = false) String page) throws UnsupportedEncodingException
	{

		name = new String(name.getBytes("ISO-8859-1"),"UTF-8");
		cv = new String(cv.getBytes("ISO-8859-1"),"UTF-8");
		motivationLettre = new String(motivationLettre.getBytes("ISO-8859-1"),"UTF-8");
		description = new String(description.getBytes("ISO-8859-1"),"UTF-8");
		
		actionService.add(name, new GregorianCalendar(), realized, cv, motivationLettre, description, actionType, actionState, communicationType,processId,contactId);

		if(page!=null)
		{
			if(page.equals("company"))
				return "redirect:company?id="+companyId;
			else if(page.equals("process"))
				return "redirect:process?id="+processId;	
			if(page.equals("contact"))
				return "redirect:contact?id="+contactId;				
		}
		return "redirect:actionsList";
	}


	@RequestMapping("/updAction")
	public ModelAndView updAction(@RequestParam(value = "id") Long id,
			@RequestParam(value = "compId", required = false) Long compId,
			@RequestParam(value = "procId", required = false) Long procId,
			@RequestParam(value = "contId", required = false) Long contId)
	{

		ModelAndView mav = new ModelAndView("action/updAction");
		if(procId != null )
		{
			mav.addObject("process",processRepository.findById(procId));
			if(compId != null )
			{
				mav.addObject("company",companyRepository.findById(compId));
			}
		}
		else if(contId != null)
		{
			mav.addObject("contact",contactRepository.findById(contId));			
		}
		else
		{
			mav.addObject("processes",processRepository.findByUser());
		}

		mav.addObject("action",actionRepository.findById(id));
		mav.addObject("processes",processRepository.findByUser());
		mav.addObject("contacts",contactRepository.findByUser());		
		return mav;		
	}

	@RequestMapping("/updActionValidate")
	public String updActionValidate(@RequestParam(value = "id") Long id,
			@RequestParam(value = "name") String name,
			@RequestParam(value = "realized") int realized,
			@RequestParam(value = "cv") String cv,
			@RequestParam(value = "motivationLettre") String motivationLettre,
			@RequestParam(value = "description") String description,
			@RequestParam(value = "actionType") ActionType actionType,
			@RequestParam(value = "actionState") ActionState actionState,
			@RequestParam(value = "communicationType") CommunicationType communicationType,
			@RequestParam(value = "processId") Long processId,
			@RequestParam(value = "contactId") Long contactId,
			@RequestParam(value = "companyId", required = false) Long companyId,
			@RequestParam(value = "page", required = false) String page) throws UnsupportedEncodingException
	{


		name = new String(name.getBytes("ISO-8859-1"),"UTF-8");
		cv = new String(cv.getBytes("ISO-8859-1"),"UTF-8");
		motivationLettre = new String(motivationLettre.getBytes("ISO-8859-1"),"UTF-8");
		description = new String(description.getBytes("ISO-8859-1"),"UTF-8");
		
		
		actionService.update(id,name, new GregorianCalendar(), realized, cv, motivationLettre, description, actionType, actionState, communicationType,processId,contactId);
		if(page!=null)
		{
			if(page.equals("company"))
				return "redirect:company?id="+companyId;
			else if(page.equals("process"))
				return "redirect:process?id="+processId;	
			if(page.equals("contact"))
				return "redirect:contact?id="+contactId;				
		}
		return "redirect:actionsList";
	}

}